package se.webbzon.boltzmann.editor.component.characterlist;

import java.awt.Dimension;
import java.util.Map;

import javax.swing.JList;
import javax.swing.JScrollPane;

import se.webbzon.boltzmann.editor.component.AbstractComponent;
import se.webbzon.boltzmann.editor.list.SortedListModel;
import se.webbzon.boltzmann.game.character.AbstractCharacterTextureSet;

public class CharacterList extends AbstractComponent {
	
	// Auto generated serial version UID
	private static final long serialVersionUID = -7338534774987132825L;
	
	// A list of all available characters in this panel
	private final JList<CharacterEntry> characterList;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public CharacterList( 	Map<String,AbstractCharacterTextureSet> characterMap,
									String characterTextureSetName) {
		super("Character");
		
		SortedListModel<CharacterEntry> characterListModel =
				new SortedListModel<CharacterEntry>(CharacterEntry.class);
		
		characterList = new JList<CharacterEntry>(characterListModel);
		
		characterList.setCellRenderer(new CharacterListCell(new Dimension(480,64)));
		JScrollPane scrollPane = new JScrollPane(characterList);
		scrollPane.setPreferredSize(new Dimension(480,480));
		add(scrollPane);
		
		CharacterEntry select = null;
		for (Map.Entry<String, AbstractCharacterTextureSet> characterEntry : characterMap.entrySet()) {
			final CharacterEntry c = new CharacterEntry(characterEntry);
			characterListModel.addElement(c);
			if (characterEntry.getKey().equals(characterTextureSetName))
				select = c;
		}
		if (select != null)
			characterList.setSelectedValue(select, true);
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the selected texture set name from this panel. **/
	public String getTextureSetName() {
		final CharacterEntry entry = characterList.getSelectedValue();
		if (entry == null)
			return "";
		else
			return entry.getCharacterTextureSetName();
	}

	/** Returns the selected texture from this panel. **/
	public AbstractCharacterTextureSet getTextureSet() {
		final CharacterEntry entry = 
				characterList.getSelectedValue();
		if (entry == null)
			return null;
		else
			return entry.getCharacterTextureSet();
	}

}
